﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using DataDefine;
using ECANPort;
using HandyControl.Controls;
using Prism.Commands;
using Prism.Events;
using Prism.Ioc;
using Prism.Mvvm;
using Prism.Regions;
using UsingEventAggregator.Core;


namespace TLDNs.ViewModels
{

    public class TestFunctionMsg : BindableBase
    {
        public int Index { get; set; }
        public uint CanIndex { get; set; }
        public CAN_OBJ Info { get; set; }
        public string DataHexString { get; set; }

        public TestFunctionMsg(int index, uint canIndex, CAN_OBJ msg)
        {
            Index = index;
            CanIndex = canIndex;
            Info = msg;
            byte[] data = new byte[msg.DataLen];
            if (msg.DataLen != 8 && msg.DataLen > 0)
            {
                Array.Copy(msg.data, data, msg.DataLen);
            }
            else
            {
                data = msg.data;
            }
            DataHexString = PubMod.ArrayToHexString(data);
        }
    }

    public class TestCanObject : BindableBase
    {
        private uint _ID = 0;
        /// <summary>
        /// 报文帧ID
        /// </summary>
        public uint CanID
        {
            get { return _ID; }
            set { SetProperty(ref _ID, value); }
        }


        private string _CanIDHex;
        public string CanIDHex
        {
            get { return _CanIDHex; }
            set
            {
                uint tmp;
                if (PubMod.HexStringToUint(value, out tmp))
                {
                    SetProperty(ref _CanIDHex, value.ToUpper());
                    CanID = tmp;
                }

            }
        }

        private int _CanIDHexLen = 3;
        public int CanIDHexLen
        {
            get { return _CanIDHexLen; }
            set { SetProperty(ref _CanIDHexLen, value); }
        }

        private int _SendType;
        /// <summary>
        /// 发送帧类型
        /// =0时为正常发送，
        /// =1时为单次发送（不自动重发），
        /// =2时 为自发自收（用于测试CAN卡是否损坏），
        /// =3时为单次自发自收（只发送一 次，用于自测试），
        /// 只在此帧为发送帧时有意义。
        /// </summary>
        public int SendType
        {
            get { return _SendType; }
            set { SetProperty(ref _SendType, value); }
        }

        private int _RemoteFlag = 0;
        /// <summary>
        /// 是否是远程帧。=0时为数据帧，=1时为远程帧
        /// </summary>
        public int RemoteFlag
        {
            get { return _RemoteFlag; }
            set { SetProperty(ref _RemoteFlag, value); }
        }

        private bool _ExternFlag = false;
        /// <summary>
        /// 是否是扩展帧。=0时为标准帧（11位帧ID），=1时为扩展帧（29位帧ID）。
        /// </summary>
        public bool ExternFlag
        {
            get { return _ExternFlag; }
            set
            {
                SetProperty(ref _ExternFlag, value);
                if (value)
                {
                    CanIDHexLen = 8;
                }
                else
                {
                    CanIDHexLen = 3;
                    if (_CanIDHex.Length > 3)
                        CanIDHex = _CanIDHex.Substring(0, 3);
                }
            }
        }


        private string _data;
        public string data
        {
            get { return _data; }
            set
            {
                System.Text.RegularExpressions.Regex re;
                string tmp = value.Trim().ToUpper();
                re = new System.Text.RegularExpressions.Regex(@"[^\s0-9A-F]{1}");
                if (re.IsMatch(tmp) == false)
                {
                    re = new System.Text.RegularExpressions.Regex(@"[0-9A-F]{1}");
                    var mat = re.Match(tmp);
                    NoSpaceData = "";
                    while (mat.Success)
                    {
                        NoSpaceData += mat.Value;
                        mat = mat.NextMatch();
                    }

                    int DataLen;
                    string end = "";
                    if (NoSpaceData.Length % 2 == 1)
                    {
                        DataLen = ((NoSpaceData.Length - 1) / 2);
                        end = NoSpaceData.Substring(DataLen * 2, 1);
                    }
                    else
                    {
                        DataLen = ((NoSpaceData.Length) / 2);
                    }
                    int nsindex = 0;
                    string fm = "";
                    for (int i = 0; i < DataLen; i++)
                    {
                        string bytehex = NoSpaceData.Substring(nsindex, 2);
                        nsindex += 2;
                        fm += bytehex + " ";
                    }
                    fm += end;
                    SetProperty(ref _data, fm);
                }
            }
        }

        /// <summary>
        /// 没有空格
        /// </summary>
        private string NoSpaceData = "";


        public CAN_OBJ GetCAN_OBJ()
        {
            CAN_OBJ info = new CAN_OBJ();
            info.ID = CanID;
            info.RemoteFlag = (byte)RemoteFlag;
            info.TimeFlag = 0;
            info.TimeStamp = 0;
            info.Reserved = new byte[3];
            info.ExternFlag = (byte)(ExternFlag ? 1 : 0);
            info.SendType = (byte)SendType;

            if (info.RemoteFlag == 0)
            {
                info.DataLen = (byte)((NoSpaceData.Length) / 2);
                info.data = new byte[info.DataLen];
                int nsindex = 0;
                for (int i = 0; i < info.DataLen; i++)
                {
                    string bytehex = NoSpaceData.Substring(nsindex, 2);
                    nsindex += 2;
                    byte tmp = 0;
                    try
                    {
                        tmp = Convert.ToByte(bytehex, 16);
                        info.data[i] = tmp;
                    }
                    catch (Exception ex)
                    {
                        info.data[i] = 0;
                    }
                }
                return info;
            }
            else
            {
                return info;
            }
        }


    }

    class TestFunctionsViewModel : CanDevViewModelBase
    {
        public TestFunctionsViewModel(IContainerExtension Container, IRegionManager rm, IEventAggregator ea) : base(Container, rm, ea)
        {
            SFM = SFMDocument.Create();

            CanDev.ECAN_RecvDataEvent += CanDev_ECAN_RecvDataEvent;
            //CanDev.ECAN_ReportStringErrEvent += CanDev_ECAN_ReportStringErrEvent;
            _ea.GetEvent<TestUIVisibilityEvent>().Subscribe(ShowTestUI);
            TestUIVisibility = Properties.Settings.Default.TestUIVisibility;

            SendTypeColl = new ObservableCollection<string>();
            SendTypeColl.AddRange(Enum.GetNames(typeof(E_SendType)));
            RemoteFlagColl = new ObservableCollection<string>();
            RemoteFlagColl.AddRange(Enum.GetNames(typeof(E_RemoteFlag)));
            TestCanInfo = new TestCanObject();
            _OpenTestLog = Properties.Settings.Default.OpenTestLog;
            _TestLog_Hz_S = Properties.Settings.Default.TestLog_Hz_S;
            TLDCanDataSet.PumpUpCmds = new Action<ushort>((v) =>
            {

                PumpCmds = v;
            });
            TLDCanDataSet.ZhuUpCmds = new Action<ushort>((v) =>
            {

                ZhuCmds = v;
            });

            TLDCanDataSet.ZhuUpStatus = new Action<ushort>((v) =>
            {
                ZhuStatus = v;
            });
            TLDCanDataSet.PumpUpStatus = new Action<ushort>((v) =>
            {
                PumpStatus = v;

            });


        }

        private bool _OpenTestLog;
        public bool OpenTestLog
        {
            get { return _OpenTestLog; }
            set
            {
                SetProperty(ref _OpenTestLog, value);
                Properties.Settings.Default.OpenTestLog = value;
            }
        }
        private int _TestLog_Hz_S;
        public int TestLog_Hz_S
        {
            get { return _TestLog_Hz_S; }
            set
            {
                SetProperty(ref _TestLog_Hz_S, value);
                MainWindowViewModel.TestLog_Hz_S = Properties.Settings.Default.TestLog_Hz_S = value;
            }
        }


        public ObservableCollection<string> _SendTypeColl;
        public ObservableCollection<string> SendTypeColl
        {
            get { return _SendTypeColl; }
            set { SetProperty(ref _SendTypeColl, value); }
        }

        public ObservableCollection<string> _RemoteFlagColl;
        public ObservableCollection<string> RemoteFlagColl
        {
            get { return _RemoteFlagColl; }
            set { SetProperty(ref _RemoteFlagColl, value); }
        }


        private SFMDocument _SFM;
        public SFMDocument SFM
        {
            get { return _SFM; }
            set
            {
                SetProperty(ref _SFM, value);
            }
        }

        private TestCanObject _TestCanInfo;
        public TestCanObject TestCanInfo
        {
            get { return _TestCanInfo; }
            set { SetProperty(ref _TestCanInfo, value); }
        }

        private void ShowTestUI(bool obj)
        {
            if (obj)
                TestUIVisibility = Visibility.Visible;
            else
                TestUIVisibility = Visibility.Hidden;
            Properties.Settings.Default.TestUIVisibility = TestUIVisibility;
        }

        private ObservableCollection<TestFunctionMsg> _MsgList = new ObservableCollection<TestFunctionMsg>();
        public ObservableCollection<TestFunctionMsg> MsgtList
        {
            get { return _MsgList; }
            set { SetProperty(ref _MsgList, value); }
        }


        public DelegateCommand DataTestSendCommand => new DelegateCommand(DataTestSendCommand_Sub);
        private void DataTestSendCommand_Sub()
        {
            CanSend(TestCanInfo.GetCAN_OBJ());
        }

        private void CanSend(CAN_OBJ frameinfo)
        {
            (bool ok, CanCom_HTB HTB, int RefIndex) rt;
            Console.WriteLine(frameinfo.ToString());
            if (Can1IsOnline)
            {
                rt = CanDev.ComPort.Get_HTBByValue(0);
                if (rt.ok)
                {
                    if (CanDev.SendMsg(ref CanDev.ComPort.HTBs[rt.RefIndex], frameinfo))
                    {
                        //_ea.GetEvent<CanWriteCountChangeEvent>().Publish();
                    }
                }
                return; //Can1 发了Can2就不发了 
            }

            if (Can2IsOnline)
            {
                rt = CanDev.ComPort.Get_HTBByValue(1);
                if (rt.ok)
                {
                    if (CanDev.SendMsg(ref CanDev.ComPort.HTBs[rt.RefIndex], frameinfo))
                    {
                        //_ea.GetEvent<CanWriteCountChangeEvent>().Publish();
                    }
                }
            }
        }


        private void CanDev_ECAN_ReportStringErrEvent(string mErrInfo, uint CanIndex)
        {
            //MsgtList.Add(new TestFunctionMsg(CanIndex, mErrInfo));
        }

        private void CanDev_ECAN_RecvDataEvent(CAN_OBJ info, uint CanIndex)
        {
            if (TextMonitor == false) return;

            var dispatcher = System.Windows.Application.Current.Dispatcher;
            if (dispatcher.CheckAccess())
            {
                MsgtList.Add(new TestFunctionMsg(MsgtList.Count, CanIndex, info));
            }
            else
            {
                dispatcher.BeginInvoke(new Action(() =>
                {
                    MsgtList.Add(new TestFunctionMsg(MsgtList.Count, CanIndex, info));
                }));
            }

            CurMsgCount++;
            if (AutoClear && CurMsgCount > AutoClearCount)
            {
                App.UIDispatcherDoAction(() => { MsgtList.Clear(); });
                CurMsgCount = 0;
            }
        }

        private static string _TestString = "TestString";
        public string TestString
        {
            get { return _TestString; }
            set { SetProperty(ref _TestString, value); }
        }

        private Visibility _TestUIVisibility = Visibility.Hidden;
        public Visibility TestUIVisibility
        {
            get { return _TestUIVisibility; }
            set { SetProperty(ref _TestUIVisibility, value); }
        }

        public DelegateCommand ClearMsgListCommand => new DelegateCommand(ClearMsgListCommand_sub);
        private void ClearMsgListCommand_sub()
        {
            MsgtList.Clear();
        }

        protected override void SFMFileIsReLoaded()
        {
            SFM = SFMDocument.Create();
        }

        public DelegateCommand StrongReflashCommand => new DelegateCommand(StrongReflashCommand_sub);
        private void StrongReflashCommand_sub()
        {
            _ea.GetEvent<RefreshCanEvent>().Publish();
            Growl.Info("强制刷新Ok!");
        }
        public DelegateCommand StrongWriteToSystemCommand => new DelegateCommand(StrongWriteToSystemCommand_sub);
        private void StrongWriteToSystemCommand_sub()
        {
            _ea.GetEvent<SaveToCanSystemEvent>().Publish((true, false));
            Growl.Info("强制写入系统Ok!");
        }


        public DelegateCommand RootModelCommand => new DelegateCommand(RootModelCommand_sub);
        private bool IsRoot = false;
        private void RootModelCommand_sub()
        {
            IsRoot = !IsRoot;
            _ea.GetEvent<IntoRootModelEvent>().Publish(IsRoot);
        }

        public DelegateCommand CreateZeroSFMCommand => new DelegateCommand(CreateZeroSFMCommand_Sub);
        private void CreateZeroSFMCommand_Sub()
        {
            SFMDocument tmp = TLDCanDataSet.TLDCanDataSet_CreateZero();
            SFMDocument.SetSFMDocument(tmp);
            _ea.GetEvent<OpenSFMIsChangeEvent>().Publish();
        }


        private bool _TextMonitor = false;
        public bool TextMonitor
        {
            get { return _TextMonitor; }
            set { SetProperty(ref _TextMonitor, value); }
        }

        public DelegateCommand MonitorCommand => new DelegateCommand(MonitorCommand_sub);
        private void MonitorCommand_sub()
        {
            TextMonitor = !TextMonitor;
        }

        private bool _AutoClear = true;
        public bool AutoClear
        {
            get { return _AutoClear; }
            set { SetProperty(ref _AutoClear, value); }
        }

        private int CurMsgCount = 0;
        public int _AutoClearCount = 100;
        public int AutoClearCount
        {
            get { return _AutoClearCount; }
            set { SetProperty(ref _AutoClearCount, value); }
        }

        private string _StartStopMsg = "开始随机数据";
        public string StartStopMsg
        {
            get { return _StartStopMsg; }
            set { SetProperty(ref _StartStopMsg, value); }
        }


        public DelegateCommand WinClosingEvent => new DelegateCommand(WinClosingEvent_Sub);
        private void WinClosingEvent_Sub()
        {
            InRandomSFMDataing = false;
        }
        private bool InRandomSFMDataing = false;
        public DelegateCommand RandomSFMDataCommand => new DelegateCommand(RandomSFMDataCommand_sub);
        private void RandomSFMDataCommand_sub()
        {
            InRandomSFMDataing = !InRandomSFMDataing;
            if (InRandomSFMDataing)
            {
                StartStopMsg = "停止随机数据";
            }
            else
            {
                StartStopMsg = "开始随机数据";
            }
            Task.Run(() =>
            {
                Random rand = new Random();
                while (InRandomSFMDataing)
                {
                    for (int i = 0; i < TLDCanDataSet.InherDoc.Devices.Length; i++)
                    {
                        for (int j = 0; j < TLDCanDataSet.InherDoc.Devices[i].Parameters.Length; j++)
                        {
                            TLDCanDataSet.InherDoc.Devices[i].Parameters[j].Value = rand.Next(-999, 999);
                        }
                    }
                    Thread.Sleep(100);
                }
            });

        }

        public DelegateCommand SetSFMDataTo0Command => new DelegateCommand(SetSFMDataTo0Command_sub);
        private void SetSFMDataTo0Command_sub()
        {

            App.UIDispatcherDoAction(() =>
            {

                for (int i = 0; i < SFM.Devices.Length; i++)
                {
                    for (int j = 0; j < SFM.Devices[i].Parameters.Length; j++)
                    {
                        SFM.Devices[i].Parameters[j].Value = 0;
                    }
                }
            });
        }
        public DelegateCommand SetInnerDataTo0Command => new DelegateCommand(SetInnerDataTo0Command_sub);
        private void SetInnerDataTo0Command_sub()
        {
            for (int i = 0; i < TLDCanDataSet.InherDoc.Devices.Length; i++)
            {
                for (int j = 0; j < TLDCanDataSet.InherDoc.Devices[i].Parameters.Length; j++)
                {
                    TLDCanDataSet.InherDoc.Devices[i].Parameters[j].Value = 0;
                }
            }
        }


        public DelegateCommand SetSFMMinValueTo0Command => new DelegateCommand(SetSFMMinValueTo0Command_sub);
        private void SetSFMMinValueTo0Command_sub()
        {
            App.UIDispatcherDoAction(() =>
            {

                for (int i = 0; i < SFM.Devices.Length; i++)
                {
                    for (int j = 0; j < SFM.Devices[i].Parameters.Length; j++)
                    {
                        SFM.Devices[i].Parameters[j].Min = 0;
                    }
                }
            });
        }
        public DelegateCommand<double?> SetSFMMaxValueCommand => new DelegateCommand<double?>(SetSFMMaxValueCommand_sub);
        private void SetSFMMaxValueCommand_sub(double? val)
        {
            if (val != null)
            {
                Console.WriteLine(val.Value);
                App.UIDispatcherDoAction(() =>
                {
                    for (int i = 0; i < SFM.Devices.Length; i++)
                    {
                        for (int j = 0; j < SFM.Devices[i].Parameters.Length; j++)
                        {
                            //SFM.Devices[i].Parameters[j].Max = (decimal)val.Value;
                            Console.WriteLine();
                        }
                    }
                });
            }
        }
        private int _SFMMaxValue;
        public int SFMMaxValue
        {
            get { return _SFMMaxValue; }
            set
            {
                SetProperty(ref _SFMMaxValue, value);
                this.RaisePropertyChanged("SFMMaxValue");
                SetSFMMaxValueCommand.CanExecute(0);
                SetSFMMaxValueCommand.RaiseCanExecuteChanged();
            }
        }

        private bool CheckCanSetSFMMaxValue(double? val)
        {
            bool can = SFMMaxValue == 666;
            Console.WriteLine("CheckCanSetSFMMaxValue:" + can);
            return can;
        }
        public DelegateCommand MonitorOddCommand => new DelegateCommand(MonitorOddCommand_sub);
        private void MonitorOddCommand_sub()
        {
            _ea.GetEvent<MonitorDataEvent>().Publish(MonitorType.Pri);
        }


        #region "ZZZZ"
        private ushort _ZhuStatus;
        public ushort ZhuStatus
        {
            get { return _ZhuStatus; }
            set { SetProperty(ref _ZhuStatus, value); }
        }
        private ushort _ZhuCmds;
        public ushort ZhuCmds
        {
            get { return _ZhuCmds; }
            set { SetProperty(ref _ZhuCmds, value); }
        }
        public DelegateCommand ZhuFindZeroCommand => new DelegateCommand(ZhuFindZeroCommand_sub);
        private void ZhuFindZeroCommand_sub()
        {

            ZhuCommand_1_0(ZhuDrive.ZhuDriveHMICanID, (ushort)E_CtrlWord.SelfChecking);
        }

        public DelegateCommand ZhuCWCommand => new DelegateCommand(ZhuCWCommand_sub);
        private void ZhuCWCommand_sub()
        {
            ZhuCWEnable = true;
            ZhuCCWEnable = false;
            ZhuCommand_1_0(ZhuDrive.ZhuDriveHMICanID, (ushort)E_CtrlWord.CWCmd);
        }

        private void ZhuCommand_1_0(uint CanDevID, ushort tmpCur)
        {

            HMIHelper.HMIDateFrame data = new HMIHelper.HMIDateFrame();
            data.Addr_Start = HMIAddrs.CtrlWorkHMIAddr;
            data.ByteCount = 2;
            data.Can_Fc = Can_Fc.Write;

            data.Data0 = new byte[2];
            data.Data0[0] = (byte)(tmpCur & 0xFF);
            data.Data0[1] = (byte)(tmpCur >> 8);

            CAN_OBJ info = HMIHelper.CreateWrite(CanDevID, data);
            CanDevWriteData(info);

            Thread.Sleep(20);
            data.Data0[0] = 0;
            data.Data0[1] = 0;
            info = HMIHelper.CreateWrite(CanDevID, data);
            CanDevWriteData(info);
        }


        public DelegateCommand ZhuCCWCommand => new DelegateCommand(ZhuCCWCommand_sub);
        private void ZhuCCWCommand_sub()
        {
            ZhuCWEnable = false;
            ZhuCCWEnable = true;
            ZhuCommand_1_0(ZhuDrive.ZhuDriveHMICanID, (ushort)E_CtrlWord.CCWCmd);
        }

        public DelegateCommand ZhuStopCommand => new DelegateCommand(ZhuStopCommand_sub);
        private void ZhuStopCommand_sub()
        {
            ZhuCWEnable = true;
            ZhuCCWEnable = true;
            ZhuCommand_1_0(ZhuDrive.ZhuDriveHMICanID, (ushort)E_CtrlWord.Stop);
        }

        public DelegateCommand ZhuManualCommand => new DelegateCommand(ZhuManualCommand_sub);
        private bool _ZhuManual = false;
        public bool ZhuManual
        {
            get { return _ZhuManual; }
            set { SetProperty(ref _ZhuManual, value); }
        }

        private void ZhuManualCommand_sub()
        {
            if (ZhuManual == false)
                ZhuCommand_1(ZhuDrive.ZhuDriveHMICanID, (ushort)E_CtrlWord.ManualAutomatic);
            else
            {
                ZhuCommand_1(ZhuDrive.ZhuDriveHMICanID, 0);
                ZhuA = ZhuB = ZhuC = false;
            }


            ZhuManual = !ZhuManual;
        }

        public DelegateCommand ZhuACommand => new DelegateCommand(ZhuACommand_sub);
        private bool _ZhuA = false;
        public bool ZhuA
        {
            get { return _ZhuA; }
            set
            {
                SetProperty(ref _ZhuA, value);
                if (value == true)
                    ZhuB = ZhuC = false;
            }
        }
        private void ZhuACommand_sub()
        {
            if (ZhuManual)
            {
                if (ZhuA == false)
                    ZhuCommand_1(ZhuDrive.ZhuDriveHMICanID, (ushort)E_CtrlWord.APhaseStart + (ushort)E_CtrlWord.ManualAutomatic);
                else
                    ZhuCommand_1(ZhuDrive.ZhuDriveHMICanID, (ushort)E_CtrlWord.ManualAutomatic);
                ZhuA = !ZhuA;
            }
            else
            {
                ZhuCommand_1(ZhuDrive.ZhuDriveHMICanID, 0);
            }
        }
 
        private void ZhuCommand_1(uint CanID, ushort tmpCur)
        {

            HMIHelper.HMIDateFrame data = new HMIHelper.HMIDateFrame();
            data.Addr_Start = HMIAddrs.CtrlWorkHMIAddr;
            data.ByteCount = 2;
            data.Can_Fc = Can_Fc.Write;
            //ushort CtrlWord = (ushort)TLDCanDataSet.InherDoc.Devices[0].Parameters[127].Value;
            //CtrlWord = 1;
            data.Data0 = new byte[2];
            data.Data0[0] = (byte)(tmpCur & 0xFF);
            data.Data0[1] = (byte)(tmpCur >> 8);

            CAN_OBJ info = HMIHelper.CreateWrite(CanID, data);
            CanDevWriteData(info);
        }

        public DelegateCommand ZhuBCommand => new DelegateCommand(ZhuBCommand_sub);
        private bool _ZhuB = false;
        public bool ZhuB
        {
            get { return _ZhuB; }
            set
            {
                SetProperty(ref _ZhuB, value);
                if (value == true)
                    ZhuA = ZhuC = false;
            }
        }
        private void ZhuBCommand_sub()
        {
            if (ZhuManual)
            {
                if (ZhuB == false)
                    ZhuCommand_1(ZhuDrive.ZhuDriveHMICanID, (ushort)E_CtrlWord.BPhaseStart + (ushort)E_CtrlWord.ManualAutomatic);
                else
                    ZhuCommand_1(ZhuDrive.ZhuDriveHMICanID, (ushort)E_CtrlWord.ManualAutomatic);
                ZhuB = !ZhuB;
            }
            else
            {
                ZhuCommand_1(ZhuDrive.ZhuDriveHMICanID, 0);
            }
        }

        public DelegateCommand ZhuCCommand => new DelegateCommand(ZhuCCommand_sub);
        private bool _ZhuC = false;
        public bool ZhuC
        {
            get { return _ZhuC; }
            set
            {
                SetProperty(ref _ZhuC, value);
                if (value == true)
                    ZhuA = ZhuB = false;
            }
        }
        private void ZhuCCommand_sub()
        {
            if (ZhuManual)
            {
                if (ZhuC == false)
                    ZhuCommand_1(ZhuDrive.ZhuDriveHMICanID, (ushort)E_CtrlWord.CPhaseStart + (ushort)E_CtrlWord.ManualAutomatic);
                else
                    ZhuCommand_1(ZhuDrive.ZhuDriveHMICanID, (ushort)E_CtrlWord.ManualAutomatic);
                ZhuC = !ZhuC;
            }
            else
            {
                ZhuCommand_1(ZhuDrive.ZhuDriveHMICanID, 0);
            }
        }
        public DelegateCommand ZhuFaultResetCommand => new DelegateCommand(ZhuFaultResetCommand_sub);
        private void ZhuFaultResetCommand_sub()
        {
            ZhuCommand_1_0(ZhuDrive.ZhuDriveHMICanID, (ushort)E_CtrlWord.FaultReset);
        }

        private bool _ZhuFindZeroEnable = true;
        public bool ZhuFindZeroEnable
        {
            get { return _ZhuFindZeroEnable; }
            set
            {
                SetProperty(ref _ZhuFindZeroEnable, value);
            }
        }
        private bool _ZhuCWEnable = true;
        public bool ZhuCWEnable
        {
            get { return _ZhuCWEnable; }
            set
            {
                SetProperty(ref _ZhuCWEnable, value);

            }
        }
        private bool _ZhuCCWEnable = true;
        public bool ZhuCCWEnable
        {
            get { return _ZhuCCWEnable; }
            set
            {
                SetProperty(ref _ZhuCCWEnable, value);
            }
        }





        #endregion

        #region "PPPPP"
        private ushort _PumpStatus;
        public ushort PumpStatus
        {
            get { return _PumpStatus; }
            set { SetProperty(ref _PumpStatus, value); }
        }
        private ushort _PumpCmds;
        public ushort PumpCmds
        {
            get { return _PumpCmds; }
            set { SetProperty(ref _PumpCmds, value); }
        }

        public DelegateCommand PumpFindZeroCommand => new DelegateCommand(PumpFindZeroCommand_sub);
        private void PumpFindZeroCommand_sub()
        {

            ZhuCommand_1_0(Pump.PumpHMICanID, (ushort)E_CtrlWord.SelfChecking);
        }

        public DelegateCommand PumpCWCommand => new DelegateCommand(PumpCWCommand_sub);
        private void PumpCWCommand_sub()
        {
            PumpCWEnable = true;
            PumpCCWEnable = false;
            ZhuCommand_1_0(Pump.PumpHMICanID, (ushort)E_CtrlWord.CWCmd);
        }



        public DelegateCommand PumpCCWCommand => new DelegateCommand(PumpCCWCommand_sub);
        private void PumpCCWCommand_sub()
        {
            PumpCWEnable = false;
            PumpCCWEnable = true;
            ZhuCommand_1_0(Pump.PumpHMICanID, (ushort)E_CtrlWord.CCWCmd);
        }

        public DelegateCommand PumpStopCommand => new DelegateCommand(PumpStopCommand_sub);
        private void PumpStopCommand_sub()
        {
            PumpCWEnable = true;
            PumpCCWEnable = true;
            ZhuCommand_1_0(Pump.PumpHMICanID, (ushort)E_CtrlWord.Stop);
        }

        public DelegateCommand PumpManualCommand => new DelegateCommand(PumpManualCommand_sub);
        private bool _PumpManual;
        public bool PumpManual
        {
            get { return _PumpManual; }
            set { SetProperty(ref _PumpManual, value); }
        }
        private void PumpManualCommand_sub()
        {

            if (PumpManual == false)
                ZhuCommand_1(Pump.PumpHMICanID, (ushort)E_CtrlWord.ManualAutomatic);
            else
            {
                ZhuCommand_1(Pump.PumpHMICanID, 0);
                PumpA = PumpB = PumpC = false;
            }

            PumpManual = !PumpManual;
        }

        public DelegateCommand PumpACommand => new DelegateCommand(PumpACommand_sub);
        private bool _PumpA = false;
        public bool PumpA
        {
            get { return _PumpA; }
            set
            {
                SetProperty(ref _PumpA, value);
                if (value == true)
                    PumpB = PumpC = false;
            }
        }
        private void PumpACommand_sub()
        {
            if (PumpManual)
            {
                if (PumpA == false)
                    ZhuCommand_1(Pump.PumpHMICanID, (ushort)E_CtrlWord.APhaseStart + (ushort)E_CtrlWord.ManualAutomatic);
                else
                    ZhuCommand_1(Pump.PumpHMICanID, (ushort)E_CtrlWord.ManualAutomatic);
                PumpA = !PumpA;
            }
            else
            {
                ZhuCommand_1(Pump.PumpHMICanID, 0);
            }
        }


        public DelegateCommand PumpBCommand => new DelegateCommand(PumpBCommand_sub);
        private bool _PumpB = false;
        public bool PumpB
        {
            get { return _PumpB; }
            set
            {
                SetProperty(ref _PumpB, value);
                if (value == true)
                    PumpA = PumpC = false;
            }
        }
        private void PumpBCommand_sub()
        {
            if (PumpManual)
            {
                if (PumpB == false)
                    ZhuCommand_1(Pump.PumpHMICanID, (ushort)E_CtrlWord.BPhaseStart + (ushort)E_CtrlWord.ManualAutomatic);
                else
                    ZhuCommand_1(Pump.PumpHMICanID, (ushort)E_CtrlWord.ManualAutomatic);
                PumpB = !PumpB;
            }
            else
            {
                ZhuCommand_1(Pump.PumpHMICanID, 0);
            }
        }

        public DelegateCommand PumpCCommand => new DelegateCommand(PumpCCommand_sub);
        private bool _PumpC = false;
        public bool PumpC
        {
            get { return _PumpC; }
            set
            {
                SetProperty(ref _PumpC, value);
                if (value == true)
                    PumpA = PumpB = false;
            }
        }

        private void PumpCCommand_sub()
        {
            if (PumpManual)
            {
                if (PumpC == false)
                    ZhuCommand_1(Pump.PumpHMICanID, (ushort)E_CtrlWord.CPhaseStart + (ushort)E_CtrlWord.ManualAutomatic);
                else
                    ZhuCommand_1(Pump.PumpHMICanID, (ushort)E_CtrlWord.ManualAutomatic);
                PumpC = !PumpC;
            }
            else
            {
                ZhuCommand_1(Pump.PumpHMICanID, 0);
            }
        }

        public DelegateCommand PumpFaultResetCommand => new DelegateCommand(PumpFaultResetCommand_sub);
        private void PumpFaultResetCommand_sub()
        {
            ZhuCommand_1_0(Pump.PumpHMICanID, (ushort)E_CtrlWord.FaultReset);
        }
        private bool _PumpFindZeroEnable = true;
        public bool PumpFindZeroEnable
        {
            get { return _PumpFindZeroEnable; }
            set { SetProperty(ref _PumpFindZeroEnable, value); }
        }

        private bool _PumpCWEnable = true;
        public bool PumpCWEnable
        {
            get { return _PumpCWEnable; }
            set
            {
                SetProperty(ref _PumpCWEnable, value);

            }
        }
        private bool _PumpCCWEnable = true;
        public bool PumpCCWEnable
        {
            get { return _PumpCCWEnable; }
            set
            {
                SetProperty(ref _PumpCCWEnable, value);
            }
        }
        #endregion

    }
}
